package com.aimee.servlet;

import com.aimee.dao.LunboDAO;
import com.aimee.entity.Lunbo;
import com.aimee.util.ReadExcel;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 公告轮播图的管理
 */
@WebServlet("/admin/lunbo")
public class LunboServlet extends BaseServlet {
    // 上传配置
    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
    LunboDAO dao=new LunboDAO();
    //查一页轮播数据 http://localhost:8080/OnlineExam/admin/lunbo?m=page
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("查一页数据");
        List adverlist=dao.show();
        request.setAttribute("adverpage", adverlist);
        System.out.println(adverlist);
        request.getRequestDispatcher("/admin/publicNotice.jsp").forward(request, response);

    }
//找出轮播图地址
    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("find轮播图数据");
        List adverlistaddress=dao.show();
        request.setAttribute("adverlistaddress", adverlistaddress);
        System.out.println(adverlistaddress);
        request.getRequestDispatcher("/user/lunbo.jsp").forward(request, response);
    }
    //上传轮播图
    protected void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("上传轮播图数据");
        DiskFileItemFactory factory = new DiskFileItemFactory();// 配置上传参数
        factory.setSizeThreshold(MEMORY_THRESHOLD);  // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
        factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); // 设置临时存储目录
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(MAX_FILE_SIZE); // 设置最大文件上传值
        upload.setSizeMax(MAX_REQUEST_SIZE);// 设置最大请求值 (包含文件和表单数据)
        upload.setHeaderEncoding("UTF-8");         // 中文处理

        // 如果目录不存在则创建
        String path="D:\\idea-project\\OnlineExam\\web\\advertise";
        File uploadDir = new File(path);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        //如何提取表单数据
        File storeFile=null;
        String filename=null;
        float size=0;
        String advertise_size;
        String filepath=null;
        Lunbo lunbo=null;
        try {
            List<FileItem> formitems=upload.parseRequest(request);
            if(formitems!=null&&formitems.size()>0) {
                for (FileItem item : formitems) {//username,pass,file
                    if(!item.isFormField()) {
                        filename=item.getName();//文件名
                        Long filesize=item.getSize();/* 文件大小 */
                        size=filesize/1024;
                        advertise_size=size+"kb";
                        String contentType=item.getContentType();
                        filepath=path+File.separator+filename;
                        System.out.println("文件名:"+filename);
                        System.out.println("文件大小:"+size+"kb");
                        System.out.println("文件种类:"+contentType);
                        System.out.println("上传文件路径:"+filepath);
                        lunbo=new Lunbo(filename,advertise_size);
                        //上传文件
                        storeFile=new File(filepath);
                        item.write(storeFile);
                    }

                }

                //上传成功后添加轮播图数据到数据库
                System.out.println("上传成功");
               int n=dao.add(lunbo);
                response.sendRedirect("http://localhost:8080/OnlineExam//admin/lunbo?m=page");
                System.out.println(n);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
//删除轮播数据
    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("删除轮播图数据");
        int advertise_id=Integer.parseInt(request.getParameter("advertise_id"));
        try {
            dao.del(advertise_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/lunbo?m=page");
    }
}
